package com.wiken.storeservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.TokenUtil;
import com.wiken.servicecommon.common.exception.DefaultException;
import com.wiken.storeservice.entity.Store;
import com.wiken.storeservice.entity.StoreType;
import com.wiken.storeservice.entity.Type;
import com.wiken.storeservice.feign.UserServiceFeignClient;
import com.wiken.storeservice.mapper.StoreTypeMapper;
import com.wiken.storeservice.service.IStoreTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@Service
public class StoreTypeServiceImpl extends ServiceImpl<StoreTypeMapper, StoreType> implements IStoreTypeService {

    @Autowired
    StoreServiceImpl storeService;

    @Autowired
    UserServiceFeignClient userServiceFeignClient;

    @Autowired
    TypeServiceImpl typeService;

    public boolean hasStoreKeepter(StoreType storeType, String token){
        String storeId = storeType.getStoreId();

        if(token == null)   throw new DefaultException(Code.ERROR_AUTHORIZATION_NULL, "操作失败");

        Claims claims = TokenUtil.parseToken(token);
        String userId = claims.get("userId", String.class);

        if(storeId == null) throw new DefaultException(Code.ERROR_STORETYPE_NULL, "操作失败");

        Store store = storeService.getOne(new QueryWrapper<Store>().eq("id", storeId));

        if(store == null) throw new DefaultException(Code.ERROR_STORE_NULL, "店铺不存在");

        boolean hasAdmin = userServiceFeignClient.getAdministratorById(userId);
        //判断是否是店主添加的店铺类型
        if(!store.getStorekeeperId().equals(userId) && !hasAdmin) throw new DefaultException(Code.ERROR_AUTHORIZATION, "操作失败");

        return true;
    }

    @Override
    public boolean addStoreType(StoreType storeType, String token) {
//        hasStoreKeepter(storeType, token);

        int success = baseMapper.insert(storeType);
        return success > 0;
    }

    @Override
    public boolean updateStoreType(StoreType storeType, String token) {
        hasStoreKeepter(storeType, token);

        int success = baseMapper.updateById(storeType);

        return success > 0;
    }

    @Override
    public boolean delStoreType(StoreType storeType, String token) {
        hasStoreKeepter(storeType, token);

        int success = baseMapper.deleteById(storeType);

        return success > 0;
    }

    @Override
    public List<StoreType> getStoreTypeList(Store store) {
        List<StoreType> storeTypes = baseMapper
                .selectList(new QueryWrapper<StoreType>().eq("storeId", store.getId()));

        return storeTypes;
    }

    @Override
    public boolean updateByStoreId(StoreType storeType, String token) {
        hasStoreKeepter(storeType, token);

        int success = baseMapper.update(storeType, new QueryWrapper<StoreType>().eq("store_id", storeType.getStoreId()));

        return success > 0;
    }

    @Override
    public boolean delStoreTypeByStoreId(StoreType storeType, String token) {
        hasStoreKeepter(storeType, token);

        int success = baseMapper.delete(new QueryWrapper<StoreType>().eq("store_id", storeType.getStoreId()));
        return success > 0;
    }

    @Override
    public Type getTypeByStoreId(Store store) {

        StoreType storeType = baseMapper.selectOne(new QueryWrapper<StoreType>().eq("store_id", store.getId()));

        Type type = new Type();
        type.setId(storeType.getTypeId());
        type = typeService.getById(type);
        return type;
    }
}
